// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <cantor/parser/parser.h>
#include <cantor/transform/exprs/row_expr.h>
#include <cantor/transform/exprs/range_var.h>
#include <cantor/transform/expr.h>

namespace cantor::transform {

    void TransRowExpr::resolve_ast(const cantor::parser::RowExpr*node) {
        if (!node) {
            status = turbo::data_loss_error("no expr");
            return;
        }
        if (node->columns.size() < 1 && node->sub_exprs.size() < 1) {
            status = turbo::data_loss_error("no columns and sub_exprs");
            return;
        }
        if (node->columns.size() >= 1) {
            row_expr.mutable_columns()->Reserve(node->columns.size());
            for (size_t i = 0; i < node->columns.size(); ++i) {
                auto col = row_expr.mutable_columns()->Add();
                status = TransRangeVar::resolve_to(node->columns[i], col);
                if (!status.ok()) {
                    return;
                }
            }
        }
        if (node->sub_exprs.size() >= 1) {
            row_expr.mutable_sub_exprs()->Reserve(node->sub_exprs.size());
            for (size_t i = 0; i < node->sub_exprs.size(); ++i) {
                auto sub = row_expr.mutable_sub_exprs()->Add();
                status = TransExpr::resolve_to(node->sub_exprs[i], sub);
                if (!status.ok()) {
                    return;
                }
            }
        }

        set_proto_location(node, row_expr.mutable_location());
        set_proto_status(row_expr.mutable_status());
    }

    const turbo::Status &TransRowExpr::check() {
        if (!status.ok()) {
            return status;
        }
        if (!row_expr.has_location()) {
            make_missing_required_field("location");
            return status;
        }
        if (row_expr.columns().empty() && row_expr.sub_exprs().empty()) {
            status = turbo::data_loss_error("no columns and sub_exprs");
        }
        return status;
    }

    turbo::Status TransRowExpr::resolve_to(turbo::Nonnull<const cantor::parser::RowExpr*> node, turbo::Nonnull<pb::ast::RowExpr*>out) {
        TransRowExpr translator;
        translator.resolve_ast(node);
        auto status = translator.check();
        if (!status.ok()) {
            return status;
        }
        *out = std::move(translator.row_expr);
        return status;
    }
}  // namespace cantor::transform
